package meterMonitor.controller;

import meterMonitor.biz.GatewayBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.model.Gateway;
import meterMonitor.vo.BaseResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RequestMapping("gatewayManage")
@RestController
public class GatewayManageController {

    public static final Logger logger = LoggerFactory.getLogger(GatewayManageController.class);

    @Autowired
    private GatewayBiz gatewayBiz;

    /**
     * 网关设置页面
     *
     * @param mv
     * @return
     */
    @RequestMapping("gatewayManage.do")
    public ModelAndView index(ModelAndView mv) {
        return mv;
    }

    /**
     * @return 所有网关
     */
    @RequestMapping("getAll.do")
    public List<Gateway> getAll() {
        return gatewayBiz.query(new Gateway());
    }

    /**
     * 删除某个网关
     *
     * @param gateway
     */
    @RequestMapping("delete.do")
    public BaseResponseData delete(Gateway gateway) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            gatewayBiz.delete(gateway);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("删除成功！");
        } catch (Exception e) {
            logger.error("删除网关信息异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("删除失败！");
        }
        return baseResponseData;
    }

    /**
     * 修改网关
     *
     * @param gateway
     */
    @RequestMapping("update.do")
    public BaseResponseData update(Gateway gateway) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            gatewayBiz.updateGateway(gateway);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("修改成功！");
        } catch (Exception e) {
            logger.error("修改网关信息异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("修改网关信息失败！");
        }
        return baseResponseData;
    }

    /**
     * 新增区域
     *
     * @return
     */
    @RequestMapping("add.do")
    @FireAuthority(authorityTypes = AuthorityType.Edit)
    @ResponseBody
    public BaseResponseData add(Gateway gateway) {
        BaseResponseData baseResponseData = new BaseResponseData();
        Gateway gateway1 = gatewayBiz.queryOneByIp(gateway.getIp());
        if (gateway1!=null){
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("ip已存在，请更换");
        }
        else{
            try {
                gatewayBiz.addGateway(gateway);
                baseResponseData.setCode(ResponseCode.SUCCESS);
                baseResponseData.setMessage("添加成功！");
            } catch (Exception e) {
                logger.error("新增网关信息异常！", e);
                baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
                baseResponseData.setMessage("新增网关信息失败！");
            }
        }

        return baseResponseData;
    }
}
